package com.itheima.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.utils.MyBaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


//把这个类交给spring管理
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /*@Autowired
    private HttpSession session;*/
    @Override
    public void insertFill(MetaObject metaObject) {
        //插入添加字段得默认值
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());

        /*Employee employee = (Employee) session.getAttribute("employee");*/
        metaObject.setValue("createUser", MyBaseContext.getThreadLong()/*employee.getId()*/);
        metaObject.setValue("updateUser", MyBaseContext.getThreadLong()/*employee.getId()*/);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        /*Employee employee = (Employee) session.getAttribute("employee");*/
        //插入修改公共字段得默认值
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",MyBaseContext.getThreadLong()/*employee.getId()*/);
    }
}
